package com.ruoyi.organization.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.ruoyi.common.annotation.CommunityScope;
import com.ruoyi.common.core.domain.entity.BizCommunity;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.organization.domain.vo.BirdsTree;
import com.ruoyi.organization.service.IBizCommunityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.organization.mapper.BizGridMapper;
import com.ruoyi.organization.domain.BizGrid;
import com.ruoyi.organization.service.IBizGridService;

/**
 * 网格Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-05-07
 */
@Service
public class BizGridServiceImpl implements IBizGridService 
{
    @Autowired
    private BizGridMapper bizGridMapper;

    @Autowired
    private IBizCommunityService bizCommunityService;

    /**
     * 查询网格
     * 
     * @param gridId 网格主键
     * @return 网格
     */
    @Override
    public BizGrid selectBizGridByGridId(Long gridId)
    {
        return bizGridMapper.selectBizGridByGridId(gridId);
    }

    /**
     * 查询网格列表
     * 
     * @param bizGrid 网格
     * @return 网格
     */
    @Override
    @CommunityScope(alias = "a")
    public List<BizGrid> selectBizGridList(BizGrid bizGrid)
    {
        return bizGridMapper.selectBizGridList(bizGrid);
    }

    /**
     * 新增网格
     * 
     * @param bizGrid 网格
     * @return 结果
     */
    @Override
    public int insertBizGrid(BizGrid bizGrid)
    {
        bizGrid.setCreateTime(DateUtils.getNowDate());
        return bizGridMapper.insertBizGrid(bizGrid);
    }

    /**
     * 修改网格
     * 
     * @param bizGrid 网格
     * @return 结果
     */
    @Override
    public int updateBizGrid(BizGrid bizGrid)
    {
        bizGrid.setUpdateTime(DateUtils.getNowDate());
        return bizGridMapper.updateBizGrid(bizGrid);
    }

    /**
     * 批量删除网格
     * 
     * @param gridIds 需要删除的网格主键
     * @return 结果
     */
    @Override
    public int deleteBizGridByGridIds(Long[] gridIds)
    {
        return bizGridMapper.deleteBizGridByGridIds(gridIds);
    }

    /**
     * 删除网格信息
     * 
     * @param gridId 网格主键
     * @return 结果
     */
    @Override
    public int deleteBizGridByGridId(Long gridId)
    {
        return bizGridMapper.deleteBizGridByGridId(gridId);
    }

    /**
     * 获取对饮的网格
     *
     * @param communityId
     * @return
     */
    @Override
    public List<BirdsTree> getGridListBy(Long communityId) {
        List<BirdsTree> ss= new ArrayList<>();
        BizCommunity bizCommunity = bizCommunityService.selectBizCommunityByCommunityId(communityId);
        List<BizGrid> grids = this.bizGridMapper.findAllByCommunityId(communityId);
        BirdsTree birdsTree = new BirdsTree();
        birdsTree.setId(bizCommunity.getCommunityId());
        birdsTree.setLabel(bizCommunity.getCommunityName());
        List<BirdsTree.InnerGrid> inners = new ArrayList<>();
        if (grids != null && grids.size() > 0) {

            for (BizGrid grid : grids) {
                BirdsTree.InnerGrid innerGrid = new BirdsTree.InnerGrid();
                innerGrid.setId(grid.getGridId());
                innerGrid.setLabel(grid.getGridName());
                inners.add(innerGrid);
            }
        }
        birdsTree.setChildren(inners);
        ss.add(birdsTree);
        return ss;
    }

    /**
     * 网格总数
     *
     * @return
     */
    @Override
    public long selectBizGridTotal() {
        long total = this.bizGridMapper.selectBizGridTotal();
        return total;
    }

    @Override
    public List<BizGrid> selectBizGridListNoScope(BizGrid bizGrid) {
        return bizGridMapper.selectBizGridListNoScope(bizGrid);
    }
}
